﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/*
 * This control is responsible for display department information
 */
public partial class DepartmentList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        //Create Fields in command "New"
        DetailsView dv = (DetailsView)sender;
        if (e.CommandName == "New")
        {
            for (int i = dv.Fields.Count - 1; i > 0; --i)
            {
                dv.Fields[i].Visible = true;
            }
        }
        else if (e.CommandName == "Cancel")
        {
            for (int i = dv.Fields.Count - 1; i > 0; --i)
            {
                dv.Fields[i].Visible = false;
            }
        }
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        DetailsView dv = (DetailsView)sender;
        for (int i = dv.Fields.Count - 1; i > 0; --i)
        {
            dv.Fields[i].Visible = false;
        }
    }

    protected string GetStaffBySN(object SerialNum)
    {
        StaffDB db = new StaffDB();
        StaffDetail staffInfo = db.GetStaffBySNum((int)SerialNum);
        return staffInfo.Name;
    }

    protected void GridView_dpt_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if(Utility.HasNullValue(e.NewValues))
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "输入不能为空！";
            e.Cancel = true;
            return;
        }


        DropDownList Directors = (DropDownList)(GridView_dpt.Rows[e.RowIndex].FindControl("DropDownList_Directors"));
        e.NewValues.Add("DirectorSN", Directors.SelectedValue);
       
    }

    protected void GridView_dpt_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            for (int i = 1; i < GridView_dpt.Columns.Count; i++)
            {
                GridView_dpt.Columns[i].ItemStyle.Width = 80;

            }
        }

        Utility.FormatEditRow(GridView_dpt, e);

    }


    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        if (Utility.HasNullValue(e.Values))
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "输入不能为空！";
            e.Cancel = true;
            return;
        }   
    }
}
